package com.quanyue.mechanics.complex.service.impl;

import com.quanyue.auto.util.AssertUtils;
import com.quanyue.mechanics.complex.mapper.MenusMapper;
import com.quanyue.mechanics.complex.service.MenusService;
import com.quanyue.mechanics.domain.entity.Menus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

@Service
public class MenuServiceImpl implements MenusService {
    @Autowired
    private MenusMapper menusMapper;

    @Override
    public Menus addMenus(Menus menus) {
        menus.setCreateTime(LocalDateTime.now());
        menus.setUpdateTime(LocalDateTime.now());
        menus.setDelFlag(false);
        menusMapper.insert(menus);
        return menus;
    }

    @Override
    public void updateMenus(Menus menus) {
        Menus check = menusMapper.findOne(menus.getId());
        if (check == null) throw new AssertUtils.ValidateException("error menus check");
        menus.setUpdateTime(LocalDateTime.now());
        menus.setCreateTime(check.getCreateTime());
        menus.setDelFlag(false);
        menusMapper.updateSelective(menus);
    }

    @Override
    public void deleteMenus(Integer id) {
        Menus check = menusMapper.findOne(id);
        if (check == null) throw new AssertUtils.ValidateException("error menus check");
        check.setUpdateTime(LocalDateTime.now());
        check.setDelFlag(false);
        menusMapper.updateSelective(check);
    }

    @Override
    public List <Menus> findMenus() {
        List <Menus> list = menusMapper.findAllValid();
        if (list.isEmpty()) return Collections.EMPTY_LIST;
        return list;
    }
}
